﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GameEditor.Common;
using GameEditor.Common.Controls;

namespace GameEditor.Common.InternalDialogs
{
    partial class DynamicValueDialog : Form
    {
        public DynamicValueDialog()
        {
            InitializeComponent();
            curveEditor = new CurveEditor();
            pnCurve.Controls.Add(curveEditor);
            curveEditor.Dock = DockStyle.Fill;
            cbValueType.SelectedIndex = 0;
        }
        private CurveEditor curveEditor;
        private void DynamicValue_Load(object sender, EventArgs e)
        {


        }

        private void OnValueTypeChanged(object sender, EventArgs e)
        { 
            switch (cbValueType.SelectedIndex)
            {
                case 0://固定值
                    curveEditor.EditMode = CurveEditMode.FixedValue;
                    propertyGrid1.SelectedObject = new FixedValueData { Editor = curveEditor };
                    break;
                case 1://随机值
                    curveEditor.EditMode = CurveEditMode.Noise;
                    propertyGrid1.SelectedObject = new NoiseData { Editor = curveEditor };

                    break;
                case 2://线性插值
                    curveEditor.EditMode = CurveEditMode.Linear;
                    propertyGrid1.SelectedObject = new CurveData { Editor = curveEditor };
                    break;
                case 3://样条插值
                    curveEditor.EditMode = CurveEditMode.Spline;
                    propertyGrid1.SelectedObject = new CurveData { Editor = curveEditor };
                    break;
                case 4://波形 
                    curveEditor.EditMode = CurveEditMode.Oscillate;
                    propertyGrid1.SelectedObject = new OscillateData { Editor = curveEditor };
                    break;
                case 5://空
                    curveEditor.EditMode = CurveEditMode.Null;
                    propertyGrid1.SelectedObject = null;
                    break;
            } 
        }

        public DynamicValue Value
        {
            get
            {
                switch (curveEditor.EditMode)
                {
                    case CurveEditMode.FixedValue:
                        return new FixedValue(curveEditor.Base);
                    case CurveEditMode.Noise:
                        return new RandomValue(curveEditor.Base, curveEditor.Amplitude);
                    case CurveEditMode.Linear:
                        return new CurvedLinearValue { ControlPoints = curveEditor.ControlPoints };
                    case CurveEditMode.Spline:
                        return new CurvedSplineValue { ControlPoints = curveEditor.ControlPoints };
                    case CurveEditMode.Oscillate:
                        return new OscillateValue
                        {
                            Base = curveEditor.Base,
                            Amplitude = curveEditor.Amplitude,
                            Frequency = curveEditor.Frequency,
                            Phase = curveEditor.Phase,
                            Type = curveEditor.OscillateType
                        };
                }

                return null;
            }
            set
            { 
                if (value == null)
                    return;
                if (value is FixedValue)
                {
                    curveEditor.Base = (value as FixedValue).Value;
                    cbValueType.SelectedIndex = 0;
                }
                if (value is RandomValue)
                {
                    RandomValue v = value as RandomValue;
                    curveEditor.Base = v.Base;
                    curveEditor.Amplitude = v.Amplitude;
                    cbValueType.SelectedIndex = 1;
                }
                if (value is CurvedLinearValue)
                {
                    curveEditor.ControlPoints = (value as CurvedLinearValue).ControlPoints;
                    cbValueType.SelectedIndex = 2;
                }
                if (value is CurvedSplineValue)
                {
                    curveEditor.ControlPoints = (value as CurvedSplineValue).ControlPoints;
                    cbValueType.SelectedIndex = 3;
                }
                if (value is OscillateValue)
                {
                    OscillateValue v = value as OscillateValue;
                    curveEditor.Amplitude = v.Amplitude;
                    curveEditor.Base = v.Base;
                    curveEditor.Frequency = v.Frequency;
                    curveEditor.Phase = v.Phase;
                    curveEditor.OscillateType = v.Type;

                    cbValueType.SelectedIndex = 4;
                }
            }
        }


        private class ValueData
        {
            [Browsable(false)]
            public CurveEditor Editor { get; set; }
        }

        private class FixedValueData : ValueData
        {
            [DisplayName("值")]
            public float Value
            {
                get
                {
                    return Editor.Base;
                }
                set
                {
                    Editor.Base = value;
                }
            }
        }
        private class NoiseData : ValueData
        {
            [DisplayName("平均值")]
            public float BaseValue
            {
                get
                {
                    return Editor.Base;
                }
                set
                {
                    Editor.Base = value;
                }
            }
            [DisplayName("噪音范围")]
            public float Amplitude
            {
                get
                {
                    return Editor.Amplitude;
                }
                set
                {
                    Editor.Amplitude = value;
                }
            }
        }

        private class CurveData : ValueData
        {

            [DisplayName("控制点")]
            public ControlPoint[] Amplitude
            {
                get
                {
                    return Editor.ControlPoints;
                }
                set
                {
                    Editor.ControlPoints = value;
                }
            }
        }

        private class OscillateData : ValueData
        {
            

            [DisplayName("波形函数")]
            public OscillateType OscilalteFunc 
            {
                get
                {
                    return Editor.OscillateType;
                }
                set
                {
                    Editor.OscillateType = value;
                }
            }
            [DisplayName("频率")]
            public float Frequency
            {
                get
                {
                    return Editor.Frequency;
                }
                set
                {
                    Editor.Frequency = value;
                }
            }
            [DisplayName("相位")]
            public float Phase 
            {
                get
                {
                    return Editor.Phase;
                }
                set
                {
                    Editor.Phase = value;
                }

            }
            [DisplayName("偏移")]
            public float Base 
            {
                get
                {
                    return Editor.Base;
                }
                set
                {
                    Editor.Base = value;
                }
            }
            [DisplayName("振幅")]
            public float Amplitude
            {
                get
                {
                    return Editor.Amplitude;
                }
                set
                {
                    Editor.Amplitude = value;
                }
            }
        }

        private void DynamicValueDialog_SizeChanged(object sender, EventArgs e)
        {

        }

    }
}
